package com.app.micaihu.view.army.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.view.army.contract.ArmyCreateContract
import com.app.micaihu.view.army.presenter.ArmyCreatePresenter
import com.app.utils.biz.Biz
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.army_notice_activity.*

/**
 * 军团公告
 */
class ArmyNoticeActivity : BaseMvpActivity<ArmyCreateContract.View, ArmyCreatePresenter>(), ArmyCreateContract.View {

    override fun getLayoutId() = R.layout.army_notice_activity

    override fun getPresenter() = ArmyCreatePresenter()

    override fun getBundle(bundle: Bundle) {
        armyNotice = bundle.getString(Config.Extra.desc, "")
        gid = bundle.getString(Config.Extra.id, "")
        toUid = bundle.getString(Config.Extra.toUid, "")
    }

    private var armyNotice = ""
    private var gid = ""
    private var toUid = ""
    private var max = 30
    private var isEdit = false

    override fun setData() {
        tvTitle.text = title
        BarUtils.addMarginTopEqualStatusBarHeight(rlTitle)
        tvArmyNotice.text = armyNotice
        etArmyNotice.setText(armyNotice)
        tvCount.text = "${etArmyNotice.text.toString().length}/${max}"
        if (Biz.instance.uid() == toUid) {
            tvRight.visibility = View.VISIBLE
        } else {
            tvRight.visibility = View.GONE
        }
    }
    override fun addListeners() {
        ivBack.setOnClickListener { finish() }
        tvRight.setOnClickListener {
            if (isEdit) {
                mPresenter?.editNotice(gid, etArmyNotice.text.toString())
            } else {
                isEdit = true
                tvArmyNotice.visibility = View.GONE
                llEditNotice.visibility = View.VISIBLE
                tvCount.visibility = View.VISIBLE
                tvRight.text = "保存"
            }
        }
        etArmyNotice.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                val length = s.toString().length
                tvCount.text = "${length}/${max}"
            }

        })
    }

    override fun handleEditNotice(result: BaseBean<Any?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                LiveEventBus.get(Config.EventBus.armyEditNoticeSuccess).post(true)
                finish()
            }
            ToastUtils.showShort(result.msg)
        }
    }
}